<template>
  <el-container class="home-container">
    <!--头部区域-->
    <el-header>
      <div>
        <img src="../../assets/img/logo.jpg" class="logo-style">
        <span>团队管理系统</span>
      </div>
      <div>
      <el-dropdown trigger="click" @command="handleCommand">
        <p style="color: #dddddd">
          设置<i class="el-icon-caret-bottom"></i>
        </p>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item>公告栏</el-dropdown-item>
          <el-dropdown-item divided command="myInfo">个人信息</el-dropdown-item>
          <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      </div>
    </el-header>
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">
          |||
        </div>
        <!--菜单 -->
        <el-menu background-color="#373d44" text-color="#fff" active-text-color="#409EFF" unique-opened
                 :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <!--一级菜单-->
          <el-submenu index="1">
            <!--一级菜单的模板区-->
            <template slot="title">
              <!--图标-->
              <i class="el-icon-user"></i>
              <!--文本-->
              <span>人员管理</span>
            </template>
            <!--二级菜单-->
            <el-menu-item index="/userList" @click="savePath('/userList')">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-menu"></i>
                <!--文本-->
                <span>梦之队成员</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/giveUserToGroup" @click="savePath('/giveUserToGroup')">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-menu"></i>
                <!--文本-->
                <span>加入分组</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/myGroup" @click="savePath('/myGroup')">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-menu"></i>
                <!--文本-->
                <span>我的分组</span>
              </template>
            </el-menu-item>
          </el-submenu>
          <el-submenu index="2">
            <!--一级菜单的模板区-->
            <template slot="title">
              <!--图标-->
              <i class="el-icon-edit-outline"></i>
              <!--文本-->
              <span>计划管理</span>
            </template>
            <!--二级菜单-->
            <el-menu-item index="/createPlan" @click="savePath('/createPlan')">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-menu"></i>
                <!--文本-->
                <span>创建计划</span>
              </template>
            </el-menu-item>
          </el-submenu>

          <el-submenu index="3">
            <!--一级菜单的模板区-->
            <template slot="title">
              <!--图标-->
              <i class="el-icon-document-copy"></i>
              <!--文本-->
              <span>文件管理</span>
            </template>
            <!--二级菜单-->
            <el-menu-item index="/uploadFile" @click="savePath('/uploadFile')">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-menu"></i>
                <!--文本-->
                <span>文件上传</span>
              </template>
            </el-menu-item>
            <!--二级菜单-->
            <el-menu-item index="/getDailyFile" @click="savePath('/getDailyFile')">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-menu"></i>
                <!--文本-->
                <span>日常文件</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/getProjectFile" @click="savePath('/getProjectFile')">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-menu"></i>
                <!--文本-->
                <span>项目文件</span>
              </template>
            </el-menu-item>
          </el-submenu>

          <el-submenu index="4">
            <!--一级菜单的模板区-->
            <template slot="title">
              <!--图标-->
              <i class="el-icon-reading"></i>
              <!--文本-->
              <span>图书管理</span>
            </template>
            <el-menu-item index="/getBook" @click="savePath('/getBook')">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-menu"></i>
                <!--文本-->
                <span>图书信息</span>
              </template>
            </el-menu-item>
          </el-submenu>

        </el-menu>
      </el-aside>

      <!--右侧主体内容-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>

    <div>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <el-form :model= "userInfo" label-width="100px" :rules="formRules" ref="userInfoForm">
          <el-form-item label="学号：" >
            <el-input size="small" disabled v-model="userInfo.userId"></el-input>
          </el-form-item>
          <el-form-item label="姓名：" prop="userName">
            <el-input size="small" v-model="userInfo.userName"></el-input>
          </el-form-item>
          <el-form-item label="联系电话：" prop="phone">
            <el-input size="small" v-model="userInfo.phone"></el-input>
          </el-form-item>
          <el-form-item label="码云账号：">
            <el-input size="small" v-model="userInfo.code"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="updateUserInfo">修改</el-button>
  </span>
      </el-dialog>
    </div>

  </el-container>

</template>

<script>
  export default {
    name: "Home",
    data(){
      return {
        //是否进行菜单栏折叠,false代表不折叠
        isCollapse: false,
        //被激活的连接地址
        activePath: '',
        dialogVisible: false,
        userInfo: {
          userId: '',
          userName: '',
          phone: '',
          code: ''
        },
        formRules:{
          userName: [
            {required: true, message: '请输入用户名', trigger: 'blur'}
          ],
          phone: [
            {required: true, message: '请输入电话号码', trigger: 'blur'}
          ]

        }
      }
    },
    //生命周期函数
    created() {
      //为了设置当前按钮的被点击时，显示高亮
      this.activePath = window.sessionStorage.getItem('activePath');
    },
    methods: {
      // 用户名下拉菜单选择事件
      handleCommand(command) {
        if (command == 'logout') {
          //清空session里面的缓存
          window.sessionStorage.clear();
          this.$router.push('/login');
        }
        if (command == 'myInfo'){
          var user = JSON.parse(window.sessionStorage.getItem('user'));
          this.userInfo.userId = user.userId;
          this.userInfo.userName = user.userName;
          this.userInfo.phone = user.phone;
          this.userInfo.code = user.codeAccount;
          this.dialogVisible = true;
        }
      },
      /*实现菜单的折叠与展开*/
      toggleCollapse(){
        this.isCollapse = !this.isCollapse;

      },
      //保存链接的激活状态
      savePath(urlPath){
        window.sessionStorage.setItem('activePath',urlPath);
        this.activePath = urlPath;
      },
      //更新用户信息
      updateUserInfo(){
        this.$refs.userInfoForm.validate(async valid =>{
          if (valid){
            var fd = new FormData();
            fd.append("userId",this.userInfo.userId);
            fd.append("userName",this.userInfo.userName);
            fd.append("phone",this.userInfo.phone);
            fd.append("codeAccount",this.userInfo.code);
            const {data: res} = await this.$http.post('user/updateUser',fd);
            if (res.status !== 200){
              return this.$message.error(res.msg);
            }
            this.dialogVisible = false;
            return this.$message.success("更改成功!");
          }else {
            return this.$message.error("请填写完整表单！");
          }
        })

      }

    }
  }
</script>

<style scoped lang="scss">
  .home-container{
    height: 100%;
  }
  .el-header{
    //#373d41
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #ffffff;
    font-size: 20px;
    > div{
      display: flex;
      align-items: center;
      span{
        margin-left: 15px;
      }
    }
  }

  .el-aside {
    //#373d44
    background-color: #373d41;
    .el-menu{
      border-right: none;
    }
  }

  .el-main {
    background-color: #E9EEF3;
  }
  .logo-style{
    height: 125px;
    width: 100px;
  }

  .toggle-button{
    /*#4A5064 */
    background-color: #373d41;
    font-size: 10px;
    line-height: 24px;
    color: #ffffff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }


</style>

